//
//  ZZCSetPayPwdOneController.m
//  ZZC-Finance
//
//  Created by a on 2017/8/20.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "ZZCSetPayPwdOneController.h"
#import "ZZCSetPayPwordTweController.h"
@interface ZZCSetPayPwdOneController ()
@property (copy, nonatomic) NSString *code;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@end

@implementation ZZCSetPayPwdOneController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.title = @"设置支付密码";
    
}
//下一步
- (IBAction)nextClick {
    
    ZZCSetPayPwordTweController *two = [ZZCSetPayPwordTweController new];
    two.code = self.code;
    [self.navigationController pushViewController:two animated:YES];
}

//获取验证码
- (IBAction)testClick {
    ZZCLog(@"获取验证码");

    NSString *phone = ZZCNSUserDefaults(@"userphone");
    NSDictionary *dict = @{@"phone":phone,@"type":@(6)};
    NSString *sign = [ZZCRequest getSignWithDict:dict];
    sign = [[NSString md5:sign] lowercaseString];
    NSDictionary *param = @{@"timestamp":[NSString timeStr],@"sign":sign,@"phone":phone,@"type":@(6)};
    [[ZZCRequest sharedNetworkTools] POST:ZZCURL_API_SENDCODE parameters:param finished:^(id  _Nullable responseObject, NSError * _Nullable error) {
        if (error) {
            ZZCLog(@"失败-%@",error);
        }else {
            [SVProgressHUD showInfoWithStatus:responseObject[@"msg"]];
            ZZCLog(@"-%@",responseObject[@"data"]);
            self.code = [responseObject[@"data"][@"code"] stringValue];
//            self.codeTextFld.text = [responseObject[@"data"][@"code"] stringValue];
            if ([responseObject[@"code"] isEqual:@(200)]) {
                [self codeTimerAction];
            }
        }
    }];
}
- (void)codeTimerAction {
    __block int time = 60;
    __block UIButton *verifybutton = _codeBtn;
    verifybutton.enabled = NO;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(time<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                verifybutton.backgroundColor = [UIColor whiteColor];
                [verifybutton setTitle:@"获取验证码" forState:UIControlStateNormal];
                verifybutton.enabled = YES;
            });
        }else{
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                //                verifybutton.backgroundColor = [UIColor whiteColor];
                NSString *strTime = [NSString stringWithFormat:@"已发送(%d)",time];
                [verifybutton setTitle:strTime forState:UIControlStateNormal];
                verifybutton.titleLabel.textColor = [UIColor whiteColor];
            });
            time--;
        }
    });
    dispatch_resume(_timer);

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
